﻿using ATEDiagnoser;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Shapes;

namespace AteCapybara
{
    public partial class Diagnoser
    {
        public string HangsPath { get; set; } 

        public string ATEPath { get; set; }

        /// <summary>
        /// 执行系统命令
        /// </summary>
        /// <param name="para"></param>
        public void Execute_Sys(string para)
        {
            try
            {
                Process.Start(para);
            }
            catch (Exception e)
            {
                MsgShow.Error($"命令执行错误：{e.Message}");
            }
        }
        /// <summary>
        /// 执行cmd命令
        /// </summary>
        /// <param name="cmd"></param>
        public void Execute_CMD(string cmd)
        {
            Process.Start("CMD.exe", "/K " + cmd);
        }
        /// <summary>
        /// ping
        /// </summary>
        /// <param name="ping_ip"></param>
        public void Execute_Ping_t(string ping_ip)
        {
            Execute_CMD("ping -t " + ping_ip);
        }
        /// <summary>
        /// 打开文件夹或者文件
        /// </summary>
        /// <param name="path"></param>
        public void Execute_Open(string path)
        {
            if (Regex.IsMatch(path, @"\\.+\.\w{1,5}$") && File.Exists(path))
            {
                Process.Start(path);
            }
            else if (Directory.Exists(path))
            {
                Process.Start(path);
            }
            else 
            {
                MsgShow.Error($"路径不正确。请检查路径：{path}");
            }
        }


        /// <summary>
        /// 自动判断做什么
        /// </summary>
        /// <param name="para"></param>
        public void Execute(string param)
        {
            param = param.Trim();
            if (string.IsNullOrEmpty(param))
            {
                return;
            }
            //ping命令
            if (Regex.IsMatch(param, @"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"))
            {
                Execute_Ping_t(param);
            }
            //cmd命令
            else if (param.StartsWith("[cmd]"))
            {
                param = param.Replace("[cmd]", "").Trim();
                Execute_CMD(param);
            }
            //网址
            else if (Regex.IsMatch(param.ToLower(), @"^(http)|(https)"))
            {
                Execute_Sys(param);
            }
            //ate文件
            else if (param.StartsWith("[ate]"))
            {
                param = param.Replace("[ate]", "").Trim();
                Execute_Open(ATEPath + param);
            }
            //hangs文件
            else if (param.StartsWith("[hangs]"))
            {
                param = param.Replace("[hangs]", "").Trim();
                Execute_Open(HangsPath + param);
            }
            //打开图片
            else if (param.StartsWith("[pic]"))
            {
                param = param.Replace("[pic]", "").Trim();
                if (param.StartsWith("[hangs]"))
                {
                    param = param.Replace("[hangs]", "").Trim();
                    param = HangsPath + param;
                }
                PictureShow ps = new PictureShow(param);
                ps.ShowDialog();
            }
            //文件、文件夹、直接执行
            else
            {
                Execute_Sys(param);
            }
        }
    }
}
